import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.Frame;

import java.io.File;
import java.util.Random;

public class Audeo {
    public static void main(String[] args) {
        videoToMusic("video/魔女之旅.mp4");
    }
    public static void videoToMusic(String videoPath){
        System.out.println("提取音频文件");
        File file=new File(videoPath);
        //抓取资源
        FFmpegFrameGrabber frameGrabber1 = new FFmpegFrameGrabber(file);
        Frame frame;
        FFmpegFrameRecorder recorder;
        String fileName;
        try{
            frameGrabber1.start();
            fileName = "music/"+file.getName().split("\\.")[0]+".wav";
            System.out.println("--文件名-->>"+fileName);
            recorder = new FFmpegFrameRecorder(fileName,frameGrabber1.getAudioChannels());
            recorder.setFormat("wav");
            recorder.setSampleRate(frameGrabber1.getSampleRate());
            recorder.setTimestamp(frameGrabber1.getTimestamp());
            recorder.setAudioQuality(0);
            recorder.start();
            int index=0;
            while (true){
                frame=frameGrabber1.grab();
                if(frame==null){
                    System.out.println("视频处理完成");
                    break;
                }
                if(frame.samples!=null){
                    recorder.recordSamples(frame.sampleRate,frame.audioChannels,frame.samples);
                }
                System.out.println("帧值="+index);
                index++;
            }
            recorder.stop();
            recorder.release();
            frameGrabber1.stop();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
